﻿using System;
using System.Diagnostics;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Data;

namespace RCFlightTimer
{
	[Activity (Label = "RCFlightTimer", MainLauncher = true)]
	public class MainActivity : Activity
	{
		RCObject[] aircraft;
		Aircraft selectedAircraft;
		Battery selectedBattery;
		Flight currentFlight;

		private Stopwatch clock = new Stopwatch ();

		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			aircraftButton ();
			aircraftSpinner ();

			timer ();

			numberPickers ();

			resetFlightButton ();
			saveFlightButton ();
		}

		protected override void OnResume ()
		{
			base.OnResume ();

			populateSpinner ();
		}

		private void aircraftButton(){
			var aircraftButton = FindViewById<Button> (Resource.Id.aircraftButton);
			aircraftButton.Click += (sender, e) => {
				var nextactivity = new Intent(this, typeof(AircraftActivity));
				StartActivity(nextactivity);
			};
		}

		/*
		 * Sets up the spinner for the selected aircraft
		 * */
		private void aircraftSpinner(){
			Spinner aircraftSpinner = FindViewById<Spinner> (Resource.Id.aircraftSpinner);
			aircraftSpinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs> (aircraftSpinnerItemSelected);
		}

		private void populateSpinner(){
			Spinner aircraftSpinner = FindViewById<Spinner> (Resource.Id.aircraftSpinner);

			var adapter = RCObjectAdapter.getAircraftAdapter (this, out aircraft);

			aircraftSpinner.Adapter = adapter;
		}

		private void aircraftSpinnerItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
		{
			Spinner spinner = (Spinner)sender;

			//Get the selected aircraft from the list
			selectedAircraft = aircraft [e.Position] as Aircraft;
		}			

		void timer ()
		{
			Chronometer chronometer = FindViewById<Chronometer> (Resource.Id.timer);

			chronometer.Click += onTimerClick;
			chronometer.ChronometerTick += onTimerTick;
		}

		private void onTimerClick(object sender, EventArgs e){
			Chronometer chronometer = FindViewById<Chronometer> (Resource.Id.timer);

			if (currentFlight == null)
				currentFlight = new Flight ();

			if (clock.IsRunning) {
				clock.Stop ();
				chronometer.Stop ();
				currentFlight.stop = DateTime.Now;
			} else {
				clock.Start ();
				chronometer.Start ();
				currentFlight.start = DateTime.Now;
			}
		}

		private void onTimerTick(object sender, EventArgs e){
			resetChronometer ();
		}

		private void resetChronometer(){
			Chronometer chronometer = FindViewById<Chronometer> (Resource.Id.timer);

			chronometer.Text = clock.Elapsed.ToString ("mm\\:ss");
		}

		private void numberPickers ()
		{
			NumberPicker minutePicker = FindViewById<NumberPicker> (Resource.Id.minutePicker);
			NumberPicker secondPicker = FindViewById<NumberPicker> (Resource.Id.secondPicker);

			minutePicker.MaxValue = 60;
			minutePicker.MinValue = 0;

			secondPicker.MaxValue = 59;
			secondPicker.MinValue = 0;
		}

		private void resetFlightButton ()
		{
			var resetButton = FindViewById<Button> (Resource.Id.resetFlightButton);
			resetButton.Click += (sender, e) => {
				resetFlight();
			};
		}

		private void resetFlight(){
			//reset the stopwatch and chronometer
			clock.Reset();
			resetChronometer();

			//delete the current flight
			currentFlight = null;
		}

		private void saveFlightButton ()
		{
			var saveButton = FindViewById<Button> (Resource.Id.saveFlightButton);
			saveButton.Click += (sender, e) => {
				if(currentFlight != null){
					//set the aircraft and battery Id's for the flight
					currentFlight.aircraftId = selectedAircraft == null ? 0 : selectedAircraft.Id;
					currentFlight.batteryId = selectedBattery == null ? 0 : selectedBattery.Id;

					//set the flight duration
					currentFlight.flightDurationInSeconds = clock.Elapsed.TotalSeconds;

					//show popup for the flight description
					var transaction = FragmentManager.BeginTransaction();
					var dialogFragment = new FlightDescriptionFragment();
					dialogFragment.Show(transaction, "dialog_fragment");
				}
			};
		}

		public void setFlightDescription(string description){
			currentFlight.description = description;
		}

		public void saveFlight ()
		{
			if (currentFlight != null) {
				Database.Instance.AddOrUpdateFlight (currentFlight);

				//Toast to let the user know it worked
				string toast = string.Format ("Flight saved");
				Toast.MakeText (this, toast, ToastLength.Long).Show ();

				//reset the current flight
				resetFlight ();
			}
		}
	}
}


